﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace StudentList
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btViewOrRefresh_Click(object sender, EventArgs e)
        {
            string path = ValidatePath(tbFilePath.Text);
            if (string.IsNullOrEmpty(path))
                return;
            string[] studentArray = File.ReadAllLines(path);
            StudentEnviroments.LoadFromStringArray(studentArray);
            RefreshList(StudentEnviroments.GetStudentUI());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private string ValidatePath(string s)
        {
            if (File.Exists(s))
                return s;
            if (File.Exists(Environment.CurrentDirectory + "\\s.txt"))
                return Environment.CurrentDirectory + "\\s.txt";
            return string.Empty;
        }

        private void RefreshList(List<UCStudent> list)
        {
            int maxWidth = splitContainer1.Panel2.Width;
            int count = maxWidth/150;
            int maxStudentCount = list.Count;
            int rowCount = maxStudentCount/count + 1;

            tableLayoutPanel1.Controls.Clear();
            tableLayoutPanel1.ColumnCount = count;
            tableLayoutPanel1.RowCount = rowCount;
            tableLayoutPanel1.Height = rowCount*202;

            foreach (UCStudent ucStudent in list)
            {
                ucStudent.ContextMenuStrip = this.PictureContextMenuStrip1;
                tableLayoutPanel1.Controls.Add(ucStudent);

            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCStudent currStudent = PictureContextMenuStrip1.SourceControl as UCStudent;
            if (currStudent == null)
                return;
            currStudent .ReloadPic();
        }

        private void tableLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            int maxWidth = splitContainer1.Panel2.Width;
            int count = maxWidth / 150;
            int maxStudentCount = StudentEnviroments.Students.Count;
            int rowCount = maxStudentCount / count + 1;

            tableLayoutPanel1.ColumnCount = count;
            tableLayoutPanel1.RowCount = rowCount;
            tableLayoutPanel1.Height = rowCount * 202;
        }

        private void PictureContextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {

            List<UCStudent> list;
            if (string.IsNullOrEmpty(tbFilter.Text.Trim()))
                list = StudentEnviroments.GetStudentUI();
            else
                list = StudentEnviroments.GetStudentUI(tbFilter.Text.Trim());

            RefreshList(list);
        }

        private void picLocalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StudentFactory.SetDefaultPicSource("local");

        }

        private void picNetworkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StudentFactory.SetDefaultPicSource("web");
        }
    }
}
    ;